#include<iostream>
#include<cstring>
using namespace std;
const int N = 1010, M = N * N;
using pii = pair<int, int>;
#define x first
#define y second
pii q[M];
char g[N][N];
int dist[N][N];
int n, m;
int dx[] = { 0,-1,0,1 }, dy[] = { -1,0,1,0 };
void bfs()
{
    memset(dist, -1, sizeof dist);
    int hh = 0, tt = -1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (g[i][j] == '1')
            {
                dist[i][j] = 0;
                q[++tt] = { i,j };
            }
        }
    }
    while (hh <= tt)
    {
        pii t = q[hh++];
        for (int i = 0; i < 4; i++)
        {
            int sx = t.x + dx[i], sy = t.y + dy[i];
            if (sx < 0 || sx >= n || sy < 0 || sy >= m) continue;
            if (dist[sx][sy] != -1) continue;
            q[++tt] = { sx,sy };
            dist[sx][sy] = dist[t.x][t.y] + 1;
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> g[i];
    bfs();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++) cout << dist[i][j] << " ";
        cout << endl;
    }
    return 0;
}